package com.ken.checkviolationapp.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.ken.checkviolationapp.R
import com.ken.checkviolationapp.bean.ANewsBean
import com.ken.checkviolationapp.bean.BNewsBean
import com.ken.checkviolationapp.bean.TypeInterf
import com.ken.checkviolationapp.databinding.ItemNews2Binding
import com.ken.checkviolationapp.databinding.ItemNewsBinding


/**
 * @author by ken
 * Blog:
 * Date:2020/5/13
 * Description:
 */
class MyRecyclerAdapter(ctx: Context, mList: MutableList<TypeInterf>) :
    RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    private val list: List<TypeInterf> = mList
    private val context = ctx
    lateinit var view: View
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {

        view = LayoutInflater.from(context).inflate(viewType, parent, false)
        return when (viewType) {
            R.layout.item_news -> ViewHolder_A(view)
            R.layout.item_news2 -> ViewHolder_B(view)
            else -> {
                println("其他...")
                ViewHolder_B(view)
            }
        }


    }

    override fun getItemCount(): Int {
        return list.size
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is ViewHolder_A) {
            val news = (list[position] as ANewsBean).data
            holder.tvTitle.text = (list[position] as ANewsBean).data.title
            holder.tvAuthorName.text = (list[position] as ANewsBean).data.author_name
            holder.tvTime.text = (list[position] as ANewsBean).data.date
            Glide.with(context)
                .load(news.thumbnail_pic_s)
                .into(holder.iv1)

        } else if (holder is ViewHolder_B) {
            val news = (list[position] as BNewsBean).data
            holder.tvTitle.text = (list[position] as BNewsBean).data.title
            holder.tvAuthorName.text = (list[position] as BNewsBean).data.author_name
            holder.tvTime.text = (list[position] as BNewsBean).data.date
            Glide.with(context)
                .load(news.thumbnail_pic_s)
                .into(holder.iv1)
            Glide.with(context)
                .load(news.thumbnail_pic_s02)
                .into(holder.iv2)
            Glide.with(context)
                .load(news.thumbnail_pic_s03)
                .into(holder.iv3)
        }

    }

    override fun getItemViewType(position: Int): Int {
        return list[position].getType()
    }
}